/**
 * 
 */
package leetCode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author zhong
 *
 */
public class MostCommonWord {
	public String mostCommonWord(String paragraph, String[] banned) {
		Set<String> ban = new HashSet<>();
		for (String string : banned) {
			ban.add(string);
		}
		String[] paras = paragraph.split("[!?',;.\\s+]");
		Map<String, Integer> map = new HashMap<>();
		for (String string : paras) {
			if (string.isEmpty()) {
				continue;
			}
			string = string.toLowerCase();
			if (!ban.contains(string)) {
				map.put(string, map.getOrDefault(string, 0) + 1);
			}
		}
		// 找到词频最大的
		int max = 0;
		String reString = "";
		for (String string : map.keySet()) {
			int freq = map.get(string);
			if (freq > max) {
				max = freq;
				reString = string;
			}
		}
		return reString;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String paragraph = "Bob. hIt, baLl";// "Bob hit a ball, the hit BALL flew far after it was hit.";
		String[] banned = { "bob", "hit" };// { "hit" };
		// "Bob. hIt, baLl"
		// {"bob", "hit"}
		System.out.println(new MostCommonWord().mostCommonWord(paragraph, banned));
	}

}
